package cn.school.thoughtworks.section2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PracticeC {
    Map<String, Integer> countSameElements(List<String> collection1) {
        //实现练习要求，并改写该行代码。
        Map<String, Integer> sameElement = new HashMap<String, Integer>();

        String pattern = "\\d+";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        for (int i = 0; i < collection1.size(); i++) {
            // Now create matcher object.
            Matcher m = r.matcher(collection1.get(i));

            if (m.find()) {

                Integer sum=sameElement.get(collection1.get(i).substring(0, 1));
                sameElement.put(collection1.get(i).substring(0, 1), sum == null ?
                        Integer.valueOf(m.group()) :  sum+ Integer.valueOf(m.group())
                );
            } else {
                Integer sum = sameElement.get(collection1.get(i));
                sameElement.put(collection1.get(i), sum == null ? 1 : sum + 1);
            }


        }
        return sameElement;
    }
}
